-- 
--  Stored Procedure: sp_SystemInfo_insert
--   Description: Inserts a new entry on the table SystemInfo.
--   Parameters:
--     @name - host name
--     @cpu  - cpu vendor, model and cores
--     @ram  - ram amount
--     @os   - operating system
--   Return value: id of the created entry.
--
--  ATTENTION: the user defined function 'dbo.udf_SystemInfo_find' must be created prior to this one.
-- 
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_SystemInfo_insert')
  DROP PROCEDURE sp_SystemInfo_insert
GO
CREATE PROCEDURE sp_SystemInfo_insert 
  @name varchar(50),
  @cpu varchar(100),
  @ram varchar(10),
  @os varchar(40)
AS
  DECLARE @ID int
  -- if exists return the id of the existing entry.
  SELECT @ID=dbo.udf_SystemInfo_find(@name)
  PRINT @ID
  IF @ID IS NOT NULL
  BEGIN
    RETURN @ID
  END
  ELSE
  BEGIN
	  -- else, insert new system info and return the id.
	  INSERT INTO SystemInfo 
		(Name, CPU, RAM, OperatingSystem)
		VALUES (@name, @cpu, @ram, @os)
	    
	  SELECT @ID=@@IDENTITY
	  RETURN @ID
  END
GO
